/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.web.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.UrlPathHelper;

/**
 * HttpServletRequest Util
 * 
 * @author liyuliang
 *
 */
public class HsrUtil {

  public static final Logger logger = LogManager.getLogger(HsrUtil.class);

  public static final UrlPathHelper urlPathHelper = new UrlPathHelper();

  public static HttpServletRequest getHttpServletRequest() {
    return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  }

  public static String getRequestUrl(HttpServletRequest request) {
    String schema = HsrUtil.getSchema(request);
    String host = HsrUtil.getServerName(request);
    int port = HsrUtil.getServerPort(request);
    String portString =
        (("http".equals(schema) && port == 80) || ("https".equals(schema) && port == 443)) ? ""
            : ":" + port;
    return schema + "://" + host + portString + getRequestUri(request);
  }

  public static String getRequestUrlWithoutUri(HttpServletRequest request) {
    String schema = HsrUtil.getSchema(request);
    String host = HsrUtil.getServerName(request);
    int port = HsrUtil.getServerPort(request);
    String portString =
        (("http".equals(schema) && port == 80) || ("https".equals(schema) && port == 443)) ? ""
            : ":" + port;
    return schema + "://" + host + portString;
  }

  public static String getRequestUrlWithQueryString(HttpServletRequest request) {
    String url = getRequestUrl(request);
    String queryString = request.getQueryString();
    if (StringUtils.isNotEmpty(queryString)) {
      return url + "?" + queryString;
    } else {
      return url;
    }
  }

  public static String getClientIp(HttpServletRequest request) {
    String ip = request.getHeader("X-Real-IP");
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("X-Forwarded-For");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
      ip = request.getRemoteAddr();
    }
    return ip;
  }

  public static String getRequestUri(HttpServletRequest request) {
    String uri = request.getHeader("X-Forwarded-URI");
    if (StringUtils.isEmpty(uri)) {
      uri = urlPathHelper.getOriginatingRequestUri(request);
    }
    return uri;
  }

  public static String getSchema(HttpServletRequest request) {
    String schema = request.getHeader("X-Forwarded-Proto");
    if (StringUtils.isEmpty(schema)) {
      schema = request.getScheme();
    }
    return schema;
  }

  public static String getRemoteHost(HttpServletRequest request) {
    return request.getRemoteHost();
  }

  public static String getServerName(HttpServletRequest request) {
    // printHeader(request);
    return request.getServerName();
  }

  public static void printHeader(HttpServletRequest request) {
    Enumeration<String> names = request.getHeaderNames();
    if (names != null) {
      while (names.hasMoreElements()) {
        String name = names.nextElement();
        logger.info(name + " " + request.getHeader(name));
      }
    }
  }

  public static int getServerPort(HttpServletRequest request) {
    return request.getServerPort();
  }

  public static int getRealPort(HttpServletRequest request) {
    String proxyPortString = request.getHeader("X-Proxy-Port");
    if (proxyPortString != null && !"".equals(proxyPortString)) {
      return Integer.parseInt(proxyPortString);
    } else {
      return request.getServerPort();
    }
  }

}
